﻿class World
{
	public var food_objects:Array; //register of all food objects in the level
	private var my_p_root:MovieClip = null; //container for a pointer to the root
	private var my_width:Number = 0; //height
	private var my_height:Number = 0; //width
	private var colour_to_sample:Object = new Object; // associative array to store translations
	private var food_array:Array; //container for the colours available in this world
	private var label_increment:Number = 0; //container for the current naming increment for food has to be 
	private var BASE_DEPTH:Number;
	private var target_sequence:Array;
	private var spawn_rate:Number;
	
	function World(p_root:MovieClip,food_available:Array,colour_dictionary:Object, target:Array, spawn_prob:Number,max_food:Number)
	{
		
		//trace("new World created");
		food_objects = new Array(max_food); //re-initialise food objects array to maximum food count
		my_p_root = p_root;
		my_height = Stage.height;
		my_width = Stage.width;
		BASE_DEPTH = 0;
		
		//set up food spawn rate
		spawn_rate = spawn_prob;
		
		//set up the target
		target_sequence = target;
		
		//set up the colours
		food_array = food_available; //initialise food types for the level
		
		//initialise colour-sample dictionary - this will later be customised or randomised for each world.
		colour_to_sample = colour_dictionary;	
	}
	public function clear_world(Void):Void
	{
		for (var i:Number =0; i < food_objects.length; i++)
		{
			food_objects[i].removeMovieClip(); // delete the object from memory.
		}
	}
	public function update_world(Void):Void
	{
		if (Math.random() < spawn_rate) // quick hack to slow down food production
		{
			generate_food();
		}
		remove_eaten_food();
	}
	public function get_food_array(Void):Array
	{
		var food_copy:Array = food_objects.slice();
		return (food_copy);
	}
	public function get_target_sequence(Void):Array
	{
		return (target_sequence.slice() );
	}
	private function generate_food(Void):Void
	{
		var random_x:Number = Math.round(Math.random() * (my_width/20)) * 20; // get a random x pos
		var random_y:Number = Math.round(Math.random() * (my_width/20)) * 20; // get a random y pos
		
		var random_choice = Math.round(Math.random() * 3); //get a random food choice
		
		var food_object:String = food_array[random_choice];
		var sound_object:String = colour_to_sample[food_object]; // get the associated sound for the colour (tidier)
		for(var i:Number = 0; i < food_objects.length; i++) // a fixed array size in combination with a layer recycling policy keeps all the food at the bottom level and a maximum limit on the amount available at any one time
		{
			if (food_objects[i] == undefined) //recycle depths (depth = array index + base_depth)
			{ 
				food_objects[i] = (my_p_root.attachMovie(food_object,"food"+(++label_increment),BASE_DEPTH+i,{unit_size:20,sound:sound_object,_x:random_x,_y:random_y})); //instantiate a new food object based on random sound#			
				return;
			}
		}
		//if all objects defined
		trace("full quota of food");
		food_objects[Math.round(Math.random() * food_objects.length)].eaten = true; //kill off a random bit of food
	}
	private function remove_eaten_food(Void):Void
	{
		for (var i:Number =0; i < food_objects.length; i++)
		{
			if (food_objects[i].eaten)
			{
				food_objects[i].removeMovieClip(); // delete the object from memory.
				food_objects[i] = undefined;
			}
		}
	}
}